package com.kx.kxblog.config;

import com.kx.kxblog.entity.User;
import com.kx.kxblog.enums.SystemContextKey;
import com.kx.kxblog.util.SystemContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;

import java.util.Optional;

@Configuration
public class UserIdAuditor implements AuditorAware<Integer> {
    @Override
    public Optional<Integer> getCurrentAuditor() {
        User user = (User) SystemContext.get(SystemContextKey.USER);
        if(user==null) return Optional.empty();
        return Optional.of(user.getId());
    }
}
